package com.accp.restaurant.controller;

import com.accp.restaurant.dao.DishesDao;
import com.accp.restaurant.dao.DisheslabelDao;
import com.accp.restaurant.dao.OrderdishesDao;
import com.accp.restaurant.dao.RoomDao;
import com.accp.restaurant.config.StringUtil;
import com.accp.restaurant.dao.*;
import com.accp.restaurant.entity.*;
import com.accp.restaurant.service.DishesService;
import com.accp.restaurant.service.OrderService;
import com.accp.restaurant.service.RoomService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/room")
@CacheConfig(cacheNames = "Room")
public class RoomController {

    @Autowired
    private RoomDao roomDao;
    @Autowired
    private RoomService roomService;
    @Autowired
    private DisheslabelDao dishesLabelDao;
    @Autowired
    private OrderdishesDao orderdishesDao;
    @Autowired
    private DishesService dishesService;
    @Autowired
    private DishesDao dishesDao;
    @Autowired
    private OrdersDao ordersDao;
    @Autowired
    private OrderService orderService;





    @RequestMapping("/querySearch")
    @RequiresPermissions("查看包间")
    public String toRoom(HttpServletRequest request){
        List<Room> list = roomService.querySearch();
        List<Disheslabel> dishesLabels = dishesLabelDao.findAll();
        request.getSession().setAttribute("list",list);
        request.getSession().setAttribute("dishesLabels",dishesLabels);
        return "redirect:/Room/RoomList.jsp";
    }

    @RequestMapping(value = "/query")
    @ResponseBody
    @RequiresPermissions("查看包间")
    public Map<String,Object> query(@RequestParam(value = "pageNumber",defaultValue = "1",required = false)Integer pageNumber,@RequestParam(value = "pageSize",defaultValue = "5",required = false) Integer pageSize, Room room, Integer roomtyperoomtypeid, Integer tabinput){
        Roomtype roomtype =new Roomtype();
        roomtype.setRoomtypeid(roomtyperoomtypeid);
        room.setRoomtype(roomtype);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("rows",roomService.query(pageNumber,pageSize,room,tabinput));
        map.put("total",roomService.getCount(pageNumber,pageSize,room,tabinput));
        return map;
    }


    @RequestMapping("/del")
    @RequiresPermissions("删除包间")
    @ResponseBody
    public String del(Room room){
        String str = "";
        try {
            roomDao.delete(room);
            str = "ok";
        } catch (Exception e) {
            str = "no";
            e.printStackTrace();
            return str;
        }
        return str;
    }
    @RequestMapping("/OpenBill")
    @ResponseBody
    @RequiresPermissions("开单")
    public String OpenBill(String arry){
        String[] a = arry.split(",");
        Long b[] = new Long[a.length];
        Room room ;
        for(int i=0;i<a.length;i++){
            b[i] = Long.parseLong(a[i]);
            room = roomDao.findByRoomid(b[i]);
            room.setRoomstate(1);
            roomDao.save(room);
        }
        return "ok";
    }


    /**
     * 获取点菜列表
     * @return
     */
    @RequestMapping(value = "/GetRoomMenus")
    @ResponseBody
    public List<Orderdishes> GetMenusByRoomid(int id){
           List<Orderdishes> list= orderdishesDao.findByRoom_Roomid(id);
           return list;
    }


    @RequestMapping("/OrderDishes")
    @ResponseBody
    @RequiresPermissions("点菜")
    public Map<String,Object> OrderDishes(long roomid,Long disheslabelid, String dishesname){
        List<Orderdishes> orderdishes = orderdishesDao.findByRoom_Roomid(roomid);
        Room r = roomDao.findById(roomid).get();
        r.setRoomstate(1);
        roomDao.save(r);
        Map<String,Object> map = new HashMap<String,Object>();
        if(orderdishes.size()!=0){
            List<Dishes> dishes = dishesService.getOrderdishes(disheslabelid,dishesname,roomid);
            map.put("orderdishes",orderdishes);
            map.put("dishes",dishes);
            return map;
        }else{
            List<Dishes> dishes = dishesService.getOrderdishes(disheslabelid,dishesname,roomid);
            map.put("dishes",dishes);
            return map;
        }
    }
    /**
     * 去结账
     */
    @RequestMapping("/ToCheckOut")
    @ResponseBody
    @RequiresPermissions("结账")
    public List<Orderdishes> ToCheckOut(String roomid,HttpServletRequest request){
        String [] a = roomid.split(",");
        Map<String,Object> map=new HashMap<>();
        List<Orderdishes> list = new ArrayList<Orderdishes>();
        List<Orderdishes> list1 = new ArrayList<Orderdishes>();
        Orderdishes orderdishes = new Orderdishes();
        for(int i=0;i<a.length;i++){
            list = orderdishesDao.findByRoom_Roomid(Integer.parseInt(a[i]));
            if(list.size()!=0){
                for (int s=0;s<list.size();s++)
                    list1.add(list.get(s));
            }else{
                Room room = roomDao.findById(Long.parseLong(a[i])).get();
                orderdishes.setRoom(room);
                list1.add(orderdishes);
            }
        }
        return list1;
    }

    /**
     * 结账
     * @return
     */
    @RequestMapping("/CheckOut")
    @ResponseBody
    public String CheckOut(String roomid){
//        String[] a = orderdishesid.split(",");
//        String[] b = roomid.split(",");
//        int[] arry = new int[a.length];
//        for(int i=0;i<a.length;i++){
//            arry[i] = Integer.parseInt(a[i]);
//        }
        String[] c = roomid.split(",");
        long[] arry = new long[c.length];
        for(int i=0;i<c.length;i++){
            arry[i] = Integer.parseInt(c[i]);
            System.out.println(arry[i]+"----------------------------------------------");
        }
        Orderdishes orderdishes = null;
        String orderid = StringUtil.geneOrderId();
        Orders orders = new Orders();
        Dishes dishes = new Dishes();
        Room room = new Room();
        for(int i=0;i<arry.length;i++){
            List<Orderdishes> list = orderdishesDao.findByRoom_Roomid(arry[i]);
            if(list.size()!=0){
                for(Orderdishes od:list){
                    System.out.println("--------------------------------------------------");
//                orders.setTotalamount(od.getDishes().getDishesprice());
//                orders.setOrderid(orderid);
//                dishes.setDishesid(od.getDishes().getDishesid());
//                orders.setDishes(dishes);
//                orders.setOrderstate(2);
                    Date date = new Date();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:ss:mm");
                    String time = simpleDateFormat.format(date);
//                orders.setOrdertime(time);
//                room.setRoomid(od.getRoom().getRoomid());
                    Room room1 = roomDao.findById(arry[i]).get();
                    room1.setRoomstate(2);
                    roomDao.save(room1);
//                orders.setRoom(room);
//                orders.setDishesnum(od.getDishesnum());
                    orderService.add(orderid,time,2,od.getDishes().getDishesid(),od.getRoom().getRoomid(),od.getDishesnum(),od.getDishes().getDishesprice());
                    orderdishesDao.deleteById(od.getOrderdishesid());
                }
            }else{
                Room room1 = roomDao.findById(arry[i]).get();
                room1.setRoomstate(2);
                roomDao.save(room1);
            }
        }
//        for(int i=0;i<arry1.length;i++){
//            for(int j=0;j<arry1.length;j++){
//                orderdishes = orderdishesDao.findAllByDishes_DishesidAndRoom_Roomid(arry1[i],arry1[j]);
//                dishes.setDishesid(orderdishes.getDishes().getDishesid());
//                order.setOrdertime(orderdishes.getDishestime());
//                room.setRoomid(orderdishes.getRoom().getRoomid());
//            }
//            order.setOrderid(Integer.toString(n).substring(0,2));
//            order.setDishesnum(order.getDishesnum());
//            order.setDishes(dishes);
//            room.setRoomstate(2);
//            order.setRoom(room);
//            order.setOrderstate(2);
//            order.setTotalamount(totalAmount);
//            System.out.println("22222222222222222222222222222222222222222222222222222222222222222222222");
//            //剩下删除菜单表的编号和修改包间表的状态
//            orderDao.save(order);
////            orderdishesDao.deleteByDishesid(arry[i]);
//        }
        return "ok";
    }


}